package com.lty.jeeg.server.modules.fas.controller;

import com.lty.jeeg.server.common.annotation.LogAnnotation;
import com.lty.jeeg.server.common.dto.ResponseInfo;
import com.lty.jeeg.server.common.enums.ResponseEnum;
import com.lty.jeeg.server.common.excel.EasyExcelUtil;
import com.lty.jeeg.server.common.utils.BeanConverter;
import com.lty.jeeg.server.common.utils.DateUtils;
import com.lty.jeeg.server.common.utils.ResponseUtil;
import com.lty.jeeg.server.common.utils.UserUtil;
import com.lty.jeeg.server.modules.fas.dto.GoodsDTO;
import com.lty.jeeg.server.modules.fas.dto.OrderDTO;
import com.lty.jeeg.server.modules.fas.export.GoodsExportEntity;
import com.lty.jeeg.server.modules.fas.export.OrderExportEntity;
import com.lty.jeeg.server.modules.fas.model.OrderDetail;
import com.lty.jeeg.server.modules.fas.service.OrderDetailService;
import com.lty.jeeg.server.modules.fas.vo.GoodsVO;
import com.lty.jeeg.server.modules.fas.vo.OrderVO;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import com.lty.jeeg.server.common.page.table.PageTableRequest;
import com.lty.jeeg.server.common.page.table.PageTableResponse;

import com.lty.jeeg.server.modules.fas.model.Order;
import com.lty.jeeg.server.modules.fas.service.OrderService;

import io.swagger.annotations.ApiOperation;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author : ZL
 * @date : 2019-02-13 10:41:32
 * Description : 需求单
 */
@Api(tags = "需求单")
@RestController
@RequestMapping("/order")
public class OrderController {

    private final OrderService orderService;

    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    public OrderController(OrderService orderService) {
        this.orderService = orderService;
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id获取")
    public ResponseInfo get(@PathVariable Long id) {
        return ResponseUtil.success(orderService.getById(id));
    }

    @PostMapping
    @ApiOperation(value = "保存")
    @RequiresPermissions("fas:order:add")
    public ResponseInfo save(@RequestBody Order order) {
        ResponseInfo info = null;
        try{
            info =  ResponseUtil.success(orderService.save(order));
        }catch (Exception e){
            info = ResponseUtil.error("保存失败请联系管理员");
        }
        return info;
    }

    @PutMapping
    @ApiOperation(value = "修改")
    @RequiresPermissions("fas:order:edit")
    public ResponseInfo update(@RequestBody Order order) {
        return ResponseUtil.success(orderService.update(order));
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    @RequiresPermissions("fas:order:del")
    public ResponseInfo delete(@PathVariable Long id) {
        return ResponseUtil.success(orderService.delete(id));
    }

    @GetMapping("/list")
    @ApiOperation(value = "列表")
    @RequiresPermissions("fas:order:query")
    public PageTableResponse list(PageTableRequest request) {
        return orderService.list(request);
    }

    @PostMapping("/approval")
    @ApiOperation(value = "审批")
    public ResponseInfo approval(@RequestParam(value = "id",required = true)Long id,@RequestParam("type")String type){
        Order order = orderService.getById(id);
        if(order==null) {
            return ResponseUtil.error(ResponseEnum.ERROR);
        }else{
            String changeType="";
            Long orderId=order.getId();
            if(!StringUtils.isEmpty(type)){
                if("submit".equals(type)){
                    changeType="1";
                }else if("withdraw".equals(type)){
                    changeType="0";
                }else if("confirm".equals(type)){
                    changeType="2";
                }else if("reject".equals(type)){
                    changeType="0";
                }
                //修改订单状态和订单明细表里的订单状态
                orderService.changeType(orderId,changeType);
                orderDetailService.changeType(orderId,changeType);
            }
            return ResponseUtil.success(ResponseEnum.SUCCESS);
        }
    }


    @LogAnnotation
    @PostMapping("/export/{type}")
    @ApiOperation(value = "商品列表导出")
    @RequiresPermissions("fas:order:export")
    public void export(@PathVariable Integer type, OrderVO orderVO, HttpServletResponse response) {
        OrderDTO orderDTO;
        if (type == 1) {
            orderDTO = BeanConverter.map(orderVO, OrderDTO.class);
        } else {
            orderDTO = new OrderDTO();
            orderDTO.setId(1L);
        }
        List<OrderExportEntity> dataList = orderService.getExportDatas(orderDTO);
        String fileName = "需求单明细列表"+ DateUtils.getDateTime("yyyyMMddHHmmss");
        EasyExcelUtil.writeExcel(response, dataList, fileName, "需求单明细", new OrderExportEntity());
    }

}